﻿using System;

namespace AirPlaneGame
{
   internal class Program
    {
        //棋盘，定义为全局变量
        private static int[] Maps = new int[100];

        //玩家的姓名
        private static string[] PlayerNames = new string[2];

        //玩家走棋的坐标
        private static int[] PlayerPos = new int[2];

        //两个玩家的状态，默认状态为false
        private static bool[] Flags = new bool[2];
        static void Main(string[] args)
        {
            #region // 1.画游戏开头  GameShow()
            GameShow();
            #endregion
            #region// 2.输入玩家的姓名（变量 PlayerName[]）
            Console.WriteLine("请输入玩家A的姓名:");
            PlayerNames[0] = Console.ReadLine();
            //检查用户输入的正确性
            while(string.IsNullOrEmpty(PlayerNames[0]))
            {
                Console.WriteLine("玩家姓名不能为空，请输入玩家A的姓名：");
                PlayerNames[0] = Console.ReadLine();
            }


            Console.WriteLine("请输入玩家B的姓名：");
            PlayerNames[1] = Console.ReadLine();
            while (string.IsNullOrEmpty(PlayerNames[1]) || PlayerNames[1] == PlayerNames[0])
            {
                if (string.IsNullOrEmpty(PlayerNames[1]))
                {
                    Console.WriteLine("玩家B的姓名不能为空，请输入玩家B的姓名：");
                    PlayerNames[1] = Console.ReadLine();
                }
                else
                {
                    Console.WriteLine("玩家B的姓名不能和玩家A相同，请输入玩家B的姓名：");
                    PlayerNames[1] = Console.ReadLine();
                }
            }
            //    打印玩家的姓名
            Console.Clear();
            GameShow();
            Console.ForegroundColor = ConsoleColor.White;
            Console.WriteLine("{0}的士兵用A表示", PlayerNames[0]);
            Console.WriteLine("{0}的士兵用B表示", PlayerNames[1]);
            #endregion
            #region// 3.初始化飞行棋地图 InitialMap()
            InitialMap();
            #endregion
            #region// 4.显示地图  DrawMap()
            DrawMap();
            #endregion
            // 5.循环走棋   只要任意玩家没有走到地图最后一个坐标  每次走棋执行相应的游戏规则 PlayGame()
            //当玩家A和玩家B没有一个人在终点的时候，两个玩家不停的去走棋
            while (PlayerPos[0] < 99 && PlayerPos[1] < 99)
            {
                //根据状态，切换玩家状态来轮流走棋
                if (!Flags[0])
                    PlayGame(0);
                else
                    Flags[0] = false;

                //判断是否游戏完成
                if (PlayerPos[0] >= 99)
                {
                    //玩家1赢，游戏结束
                    Console.WriteLine("玩家{0}无耻的赢了玩家{1}", PlayerNames[0], PlayerNames[1]);
                    break;
                }

                //根据状态，切换玩家状态来轮流走棋
                if (!Flags[1])
                    PlayGame(1);
                else
                    Flags[1] = false;

                //判断是否游戏完成
                if (PlayerPos[1] >= 99)
                {
                    //玩家1赢，游戏结束
                    Console.WriteLine("玩家{0}无耻的赢了玩家{1}", PlayerNames[1], PlayerNames[0]);
                    break;
                }
            }
            // 6.显示胜利画面
            Win();
            //结束
            Console.ReadKey();
        }
        /// <summary>
        /// 开头画面
        /// </summary>
        #region"游戏的开始"
        public static void GameShow()
        {
            Console.ForegroundColor = ConsoleColor.Yellow;
            Console.WriteLine("**************************************");
            Console.ForegroundColor = ConsoleColor.Red;
            Console.WriteLine("**************************************");
            Console.ForegroundColor = ConsoleColor.Green;
            Console.WriteLine("**************************************");
            Console.ForegroundColor = ConsoleColor.White;
            Console.WriteLine("***** 软件工程演示项目：飞行棋1.0*****");
            Console.ForegroundColor = ConsoleColor.Blue;
            Console.WriteLine("**************************************");
            Console.ForegroundColor = ConsoleColor.Cyan;
            Console.WriteLine("**************************************");
            Console.ForegroundColor = ConsoleColor.DarkRed;
            Console.WriteLine("**************************************");
        }
        #endregion
        /// <summary>
        /// 初始化棋盘
        /// </summary>
        ///
        #region"幸运轮盘"
        public static void InitialMap()
        
        {
            //幸运轮盘◎
            int[] luckyturn = { 6, 23, 40, 55, 68, 83 };
            for (var i = 0; i < luckyturn.Length; i++)
            {
                Maps[luckyturn[i]] = 1;   //值1表示幸运轮盘
            }

            //地雷☆
            int[] mines = { 5, 13, 17, 33, 38, 50, 63, 80, 95 };
            for (var i = 0; i < mines.Length; i++)
            {
                Maps[mines[i]] = 2;   //值2表示地雷
            }

            //暂停▲
            int[] pause = { 3, 9, 27, 37, 60, 84, 93 };
            for (var i = 0; i < pause.Length; i++)
            {
                Maps[pause[i]] = 3;   //值3表示暂停
            }

            //时空隧道卐
            int[] tunel = { 20, 25, 45, 62, 72, 88, 91 };
            for (var i = 0; i < tunel.Length; i++)
            {
                Maps[tunel[i]] = 4;   //值4表示时空隧道
            }
        }
        #endregion
        /// <summary>
        /// 画出地图
        /// </summary>
        #region"画图区域"
        public static void DrawMap()
        {
            Console.WriteLine("图例:幸运轮盘:◎   地雷:☆   暂停:▲   时空隧道:卐");

            //第一横行
            for (var i = 0; i < 30; i++)
                Console.Write(DrawMapPos(i));
            Console.WriteLine();

            //第一竖行
            for (var i = 30; i < 35; i++)
            {
                for (var j = 0; j < 29; j++)
                {
                    Console.Write("  ");
                }

                Console.Write(DrawMapPos(i));
                Console.WriteLine();
            }

            //第二横行
            for (var i = 63; i >= 35; i--)
                Console.Write(DrawMapPos(i));

            //第二竖行
            for (var i = 64; i <= 69; i++)
                Console.WriteLine(DrawMapPos(i));

            //第三横行
            for (var i = 70; i <= 99; i++)
                Console.Write(DrawMapPos(i));

            //结束换行
            Console.WriteLine();
        }
        #endregion
        /// <summary>
        /// 画棋盘的方法，根据坐标显示对应棋盘位置的字符
        /// </summary>
        /// <param name="i"></param>
        /// <returns></returns>
        #region"画棋盘方法"
        public static string DrawMapPos(int i)
        {
            string str = "";
            //如果A B重合  显示<>
            if (PlayerPos[0] == PlayerPos[1] && PlayerPos[0] == i)
                str = "<>";
            else if (PlayerPos[0] == i)
                str = "A";
            else if (PlayerPos[1] == i)
                str = "B";
            else
            {
                //正常位置□  幸运轮盘:◎   地雷:☆   暂停:▲   时空隧道:卐
                switch (Maps[i])
                {
                    case 0:
                        Console.ForegroundColor = ConsoleColor.Yellow;
                        str = "□";
                        break;

                    case 1:
                        Console.ForegroundColor = ConsoleColor.Green;
                        str = "◎";
                        break;

                    case 2:
                        Console.ForegroundColor = ConsoleColor.Red;
                        str = "☆";
                        break;

                    case 3:
                        Console.ForegroundColor = ConsoleColor.Blue;
                        str = "▲";
                        break;

                    case 4:
                        Console.ForegroundColor = ConsoleColor.DarkCyan;
                        str = "卐";
                        break;
                }
            }
            return str;
        }
        #endregion
        /// <summary>
        /// 玩游戏，走棋
        /// </summary>
        public static void PlayGame(int player)
        {
            //掷骰子
            var r = new Random();
            //生成随机数1-6
            int rNumber = r.Next(1, 7);
            Console.WriteLine("{0}按任意键开始掷骰子", PlayerNames[player]);
            Console.ReadKey(true);
            Console.WriteLine("{0}掷出了{1}", PlayerNames[player], rNumber);

            //走棋，玩家前进
            PlayerPos[player] += rNumber;
            //检测位置是否正确
            ChangePos();

            Console.ReadKey(true);
            Console.WriteLine("{0}按任意键开始行动", PlayerNames[player]);
            Console.ReadKey(true);
            Console.WriteLine("{0}行动完成", PlayerNames[player]);
            Console.ReadKey(true);

            //按幸运轮盘 地雷 暂停 时空隧道等状态判断玩家的行动
            if (PlayerPos[player] == PlayerPos[1 - player])
            {
                //打烧，对方玩家退6格
                Console.WriteLine("玩家{0}踩到了玩家{1},玩家{2}后退6格", PlayerNames[player], PlayerNames[1 - player], PlayerNames[1 - player]);
                PlayerPos[1 - player] -= 6;
                ChangePos();
                Console.ReadKey(true);
            }
            else
            {
                //踩到不同的位置关卡
                switch (Maps[PlayerPos[player]])
                {
                    case 0:
                        //踩到方块，正常
                        Console.WriteLine("玩家{0}踩到的是方块，行动安全", PlayerNames[player]);
                        Console.ReadKey(true);
                        break;

                    case 1:
                        //踩到幸运轮盘，1.和对方交换位置  2.打烧对方
                        Console.WriteLine("玩家{0}踩到的是幸运轮盘，请选择1--交换位置  2--轰炸对方", PlayerNames[player]);
                        var inputStr = Console.ReadLine();
                        //用户只能输入1 2，如果输入其它字符串重输入
                        while (true)
                        {
                            if (inputStr == "1")
                            {
                                Console.WriteLine("玩家{0}选择跟玩家{1}交换位置", PlayerNames[player], PlayerNames[1 - player]);
                                Console.ReadKey(true);

                                //两个玩家位置交换
                                var tmp = PlayerPos[player];
                                PlayerPos[player] = PlayerPos[1 - player];
                                PlayerPos[1 - player] = tmp;

                                Console.WriteLine("玩家{0}跟玩家{1}交换位置完毕，按任意键继续", PlayerNames[player], PlayerNames[1 - player]);
                                Console.ReadKey(true);

                                //业务完成，结束while循环
                                break;
                            }
                            else if (inputStr == "2")
                            {
                                Console.WriteLine("玩家{0}选择轰炸玩家{1}，玩家{2}退后6格", PlayerNames[player], PlayerNames[1 - player], PlayerNames[1 - player]);
                                Console.ReadKey(true);
                                //对方退后6格
                                PlayerPos[1 - player] -= 6;
                                ChangePos();
                                Console.WriteLine("玩家{0}退后了6格，按任意键继续", PlayerNames[1 - player]);
                                Console.ReadKey(true);

                                break;
                            }
                            else
                            {
                                Console.WriteLine("玩家{0}踩到的是幸运轮盘，请选择1--交换位置  2--轰炸对方", PlayerNames[player]);
                                inputStr = Console.ReadLine();
                            }
                        }
                        break;

                    case 2:
                        //踩到地雷  退6格
                        Console.WriteLine("玩家{0}飞入防空识别区被击落，退6格", PlayerNames[player]);
                        Console.ReadKey(true);
                        PlayerPos[player] -= 6;
                        ChangePos();
                        break;

                    case 3:
                        //踩到暂停，暂停一回合
                        Console.WriteLine("玩家{0}踩到暂停，暂停一回合", PlayerNames[player]);
                        Console.ReadKey(true);
                        Flags[player] = true;
                        break;

                    case 4:
                        //踩到时空隧道，前进10格
                        Console.WriteLine("玩家{0}踩到时空隧道，前进10格", PlayerNames[player]);
                        Console.ReadKey(true);
                        PlayerPos[player] += 10;
                        ChangePos();
                        break;
                }
            }
            ChangePos();
            Console.Clear();
            DrawMap();
    }

    /// <summary>
    /// 修正玩家的坐标  不能超出0-99
    /// </summary>
    public static void ChangePos()
        {
            if (PlayerPos[0] < 0)
                PlayerPos[0] = 0;
            if (PlayerPos[0] > 99)
                PlayerPos[0] = 99;

            if (PlayerPos[1] < 0)
                PlayerPos[1] = 0;
            if (PlayerPos[1] > 99)
                PlayerPos[1] = 99;
        }
        public static void Win()
        {
            Console.ForegroundColor = ConsoleColor.Red;
            Console.WriteLine("                                          ◆                      ");
            Console.WriteLine("                    ■                  ◆               ■        ■");
            Console.WriteLine("      ■■■■  ■  ■                ◆■         ■    ■        ■");
            Console.WriteLine("      ■    ■  ■  ■              ◆  ■         ■    ■        ■");
            Console.WriteLine("      ■    ■ ■■■■■■       ■■■■■■■   ■    ■        ■");
            Console.WriteLine("      ■■■■ ■   ■                ●■●       ■    ■        ■");
            Console.WriteLine("      ■    ■      ■               ● ■ ●      ■    ■        ■");
            Console.WriteLine("      ■    ■ ■■■■■■         ●  ■  ●     ■    ■        ■");
            Console.WriteLine("      ■■■■      ■             ●   ■   ■    ■    ■        ■");
            Console.WriteLine("      ■    ■      ■            ■    ■         ■    ■        ■");
            Console.WriteLine("      ■    ■      ■                  ■               ■        ■ ");
            Console.WriteLine("     ■     ■      ■                  ■           ●  ■          ");
            Console.WriteLine("    ■    ■■ ■■■■■■             ■              ●         ●");
            Console.ResetColor();
        }
    }
}
